//
//  BindPhoneView.swift
//  DollBuy
//
//  Created by 郑东喜 on 2016/11/22.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit

// MARK:- 设置代理
protocol BindPhoneDelegate {
    //接收绑定成功
    func bindSuccess()
}

class BindPhoneView: UIView {
    
    //设置代理
    var delegate : BindPhoneDelegate?
    
    
    //手机号码
    var tfNum: TfPlaceHolder = {
        let lab = TfPlaceHolder()
        lab.frame = CGRect(x: 50, y: 0, width: UIScreen.main.bounds.size.width - 60 , height: 45)
        //        lab.placeholder = "请输入您的手机号码"
        lab.plStrSize(str: "请输入您的手机号码", holderColor: UIColor.gray, textFontSize: 13)
        
        //文字左对齐
        lab.textAlignment = NSTextAlignment.left
        return lab
    }()
    
    
    //分割线
    lazy var line : UIView = {
        let lin = UIView()
        lin.frame = CGRect(x: 0, y: 45, width: UIScreen.main.bounds.size.width, height: 0.5)
        lin.backgroundColor = UIColor.lightGray
        return lin
    }()
    
    
    //验证码
    var tfAuth: TfPlaceHolder = {
        let lab = TfPlaceHolder()
        lab.frame = CGRect(x: 50, y: 45, width: UIScreen.main.bounds.size.width - 60 , height: 45)
        lab.plStrSize(str: "请输入您的验证码", holderColor: UIColor.gray, textFontSize: 13)
        
        //文字左对齐
        lab.textAlignment = NSTextAlignment.left
        return lab
    }()
    
    //密码
    var tfPass: TfPlaceHolder = {
        let lab = TfPlaceHolder()
        lab.frame = CGRect(x: 50, y: 90, width: UIScreen.main.bounds.size.width - 60 , height: 45)
        lab.plStrSize(str: "请输入您的密码", holderColor: UIColor.gray, textFontSize: 13)
        
        //文字左对齐
        lab.textAlignment = NSTextAlignment.left
        return lab
    }()
    
    //密码前置图片
    lazy var tfPassImg: UIImageView = {
        let img = UIImageView()
        //自适应放大
        img.contentMode = UIViewContentMode.scaleAspectFit
        img.frame = CGRect(x: 7.5, y: 97.5, width: 30, height: 30)
        
        img.backgroundColor = UIColor.gray
        img.contentMode = UIViewContentMode.scaleAspectFit
        return img
    }()
    
    //分割线
    lazy var tfPassLine : UIView = {
        let lin = UIView()
        lin.frame = CGRect(x: 0, y: 135, width: UIScreen.main.bounds.size.width, height: 0.5)
        lin.backgroundColor = UIColor.lightGray
        return lin
    }()
    
    
    //验证码按钮
    lazy var yzmBtn : CountDownBtn = {
        let btn = CountDownBtn()
        btn.backgroundColor = UIColor.orange
//        btn.initwith(frame: CGRect(x: SW - 15 - 100, y: 52.5, width: 100, height: 30), color: UIColor.orange, title: "获取验证码", superView: self, delegate: self)
        btn.frame = CGRect(x: SW - 15 - 100, y: 52.5, width: 100, height: 30)
        btn.backgroundColor = UIColor.orange
        btn.layer.cornerRadius = 8
        btn.setTitle("获取验证码", for: .normal)
        btn.addTarget(self, action: #selector(autoSEL(sender:)), for: .touchUpInside)
        
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        
        return btn
    }()
    
    //分割线
    lazy var newPassLine : UIView = {
        let lin = UIView()
        lin.frame = CGRect(x: 0, y: 90, width: UIScreen.main.bounds.size.width, height: 0.5)
        lin.backgroundColor = UIColor.lightGray
        return lin
    }()
    

    //手机号前置图片
    lazy var tfOldImg: UIImageView = {
        let img = UIImageView()
        //自适应放大
        img.contentMode = UIViewContentMode.scaleAspectFit
        img.frame = CGRect(x: 7.5, y: 7.5, width: 30, height: 30)
        
        img.backgroundColor = UIColor.gray
        img.contentMode = UIViewContentMode.scaleAspectFit
        return img
    }()
    
    //验证码前置图片
    lazy var tfNewImg: UIImageView = {
        let img = UIImageView()
        //自适应放大
        img.contentMode = UIViewContentMode.scaleAspectFit
        img.frame = CGRect(x: 7.5, y: 52.5, width: 30, height: 30)
        
        img.backgroundColor = UIColor.gray
        img.contentMode = UIViewContentMode.scaleAspectFit
        return img
    }()

    
    //保存按钮
    lazy var saveBtn : UIButton = {
        let btn = UIButton()
        btn.backgroundColor = UIColor.orange
        btn.frame = CGRect(x: 7.5, y: 110, width: UIScreen.main.bounds.width - 15, height: 40)
        btn.backgroundColor = UIColor.orange
        btn.layer.cornerRadius = 8
        btn.setTitle("保存", for: .normal)
         
        btn.addTarget(self, action: #selector(BindPhoneView.confirmSEL), for: .touchUpInside)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        
        return btn
    }()
    
    override func layoutSubviews() {
        super.layoutSubviews()
        
        self.setUI()
    }
    
}

extension BindPhoneView {
    func setUI() -> Void {
        
        //手机号
        addSubview(tfNum)
        addSubview(tfOldImg)
        addSubview(line)
        
        //验证码
        addSubview(tfAuth)
        addSubview(tfNewImg)
        addSubview(newPassLine)
        addSubview(yzmBtn)
    
        //密码
//        addSubview(tfPass)
//        addSubview(tfPassImg)
//        addSubview(tfPassLine)
        
        addSubview(saveBtn)
    }
}


// MARK:- 单击事件
extension BindPhoneView {
    //验证码事件
    func autoSEL(sender : CountDownBtn) -> Void {
        SendAutoModel.shared.sendForgetAuto(sendType: 3, tfNum: tfNum) { (result) in
            print(result)
            
            self.endEditing(true)
            if result == "true" {
                sender.initwith(color: UIColor.orange, title: "发送验证码", superView: self)
            }
        }
    }
    
    //确定事件
    func confirmSEL() -> Void {
        print("绑定手机号")
        

        BindPhoneModel.shared.BindPhoneSEL(tfAuto: tfAuth, tfNum: tfNum) { (result) in
            print(result)

            if result == "成功" {
                self.delegate?.bindSuccess()
                
                //弹出警告框
                let alertVC = UIAlertController.init(title: "设置密码", message: "绑定手机，方便登陆", preferredStyle: .alert)
                
                alertVC.addTextField { (tf) in
                    tf.placeholder = "请输入密码"
                }
                
                
                let okAction = UIAlertAction(title: "好的", style: .default, handler: {
                    action in
                    //也可以用下标的形式获取textField let login = alertController.textFields![0]
                    
                    let password = alertVC.textFields!.last!.text
                    print("密码：\(password)")
                    
                    //绑定手机号模型接入
                    BindPhoneSecrectModel.shared.bindSecrect(passStr: password!, comfun: { (result) in
                        print(result)
                    })
                })
                
                alertVC.addAction(okAction)
                
                alertVC.addAction(UIAlertAction.init(title: "确定", style: .default, handler: nil))
                nav?.present(alertVC, animated: true, completion: nil)
                
                //绑定账号成功后，在绑定密码
                if self.tfPass.text?.characters.count == 0 {
                    MBManager.showBriefAlert("密码不能为空")
                } else {
                    BindPhoneSecrectModel.shared.bindSecrect(passStr: self.tfPass.text!, comfun: { (result) in
                    })
                }
            }
        }
    }
}


